<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房型名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房型名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="英文名称" prop="englishName">
        <el-input
          v-model="queryParams.englishName"
          placeholder="请输入英文名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房型分类" prop="roomClass">
        <el-select v-model="queryParams.roomClass" placeholder="请选择房型分类" clearable>
          <el-option
            v-for="dict in room_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房型面积" prop="floorArea">
        <el-input
          v-model="queryParams.floorArea"
          placeholder="请输入房型面积"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最大入住人数" prop="maxOccupancy">
        <el-input
          v-model="queryParams.maxOccupancy"
          placeholder="请输入最大入住人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="窗户状态" prop="hasWindow">
        <el-select v-model="queryParams.hasWindow" placeholder="请选择窗户状态" clearable>
          <el-option
            v-for="dict in window_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in wdm_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['businessInfo:room_type:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['businessInfo:room_type:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['businessInfo:room_type:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['businessInfo:room_type:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="room_typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房型ID" align="center" prop="roomTypeId" />
      <el-table-column label="主图媒体ID" align="center" prop="mediaId" />
      <el-table-column label="房型名称" align="center" prop="roomName" />
      <el-table-column label="英文名称" align="center" prop="englishName" />
      <el-table-column label="房型分类" align="center" prop="roomClass">
        <template #default="scope">
          <dict-tag :options="room_category" :value="scope.row.roomClass"/>
        </template>
      </el-table-column>
      <el-table-column label="房型面积" align="center" prop="floorArea" />
      <el-table-column label="最大入住人数" align="center" prop="maxOccupancy" />
      <el-table-column label="窗户状态" align="center" prop="hasWindow">
        <template #default="scope">
          <dict-tag :options="window_status" :value="scope.row.hasWindow"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wdm_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['businessInfo:room_type:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['businessInfo:room_type:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房型信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="room_typeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主图媒体ID" prop="mediaId">
          <el-input v-model="form.mediaId" placeholder="请输入主图媒体ID" />
        </el-form-item>
        <el-form-item label="房型名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房型名称" />
        </el-form-item>
        <el-form-item label="英文名称" prop="englishName">
          <el-input v-model="form.englishName" placeholder="请输入英文名称" />
        </el-form-item>
        <el-form-item label="房型分类" prop="roomClass">
          <el-radio-group v-model="form.roomClass">
            <el-radio
              v-for="dict in room_category"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="房型面积" prop="floorArea">
          <el-input v-model="form.floorArea" placeholder="请输入房型面积" />
        </el-form-item>
        <el-form-item label="最大入住人数" prop="maxOccupancy">
          <el-input v-model="form.maxOccupancy" placeholder="请输入最大入住人数" />
        </el-form-item>
        <el-form-item label="总房间量" prop="totalQuantity">
          <el-input v-model="form.totalQuantity" placeholder="请输入总房间量" />
        </el-form-item>
        <el-form-item label="所在楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入所在楼层" />
        </el-form-item>
        <el-form-item label="窗户状态" prop="hasWindow">
          <el-radio-group v-model="form.hasWindow">
            <el-radio
              v-for="dict in window_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in wdm_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio
              v-for="dict in del_flag"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">床型配置信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddBedConfig">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteBedConfig">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="bedConfigList" :row-class-name="rowBedConfigIndex" @selection-change="handleBedConfigSelectionChange" ref="bedConfig">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="床型类别" prop="bedType" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.bedType" placeholder="请选择床型类别">
                <el-option
                  v-for="dict in bed_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="床尺寸" prop="bedSize" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.bedSize" placeholder="请输入床尺寸" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Room_type">
import { listRoom_type, getRoom_type, delRoom_type, addRoom_type, updateRoom_type } from "@/api/businessInfo/room_type"

const { proxy } = getCurrentInstance()
const { room_category, wdm_status, bed_type, window_status, del_flag } = proxy.useDict('room_category', 'wdm_status', 'bed_type', 'window_status', 'del_flag')

const room_typeList = ref([])
const bedConfigList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const checkedBedConfig = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    roomName: null,
    englishName: null,
    roomClass: null,
    floorArea: null,
    maxOccupancy: null,
    hasWindow: null,
    status: null,
  },
  rules: {
    roomName: [
      { required: true, message: "房型名称不能为空", trigger: "blur" }
    ],
    roomClass: [
      { required: true, message: "房型分类不能为空", trigger: "change" }
    ],
    floorArea: [
      { required: true, message: "房型面积不能为空", trigger: "blur" }
    ],
    hasWindow: [
      { required: true, message: "窗户状态不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询房型信息列表 */
function getList() {
  loading.value = true
  listRoom_type(queryParams.value).then(response => {
    room_typeList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    roomTypeId: null,
    mediaId: null,
    roomName: null,
    englishName: null,
    roomClass: null,
    floorArea: null,
    maxOccupancy: null,
    totalQuantity: null,
    floor: null,
    hasWindow: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  }
  bedConfigList.value = []
  proxy.resetForm("room_typeRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.roomTypeId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加房型信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _roomTypeId = row.roomTypeId || ids.value
  getRoom_type(_roomTypeId).then(response => {
    form.value = response.data
    bedConfigList.value = response.data.bedConfigList
    open.value = true
    title.value = "修改房型信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["room_typeRef"].validate(valid => {
    if (valid) {
      form.value.bedConfigList = bedConfigList.value
      if (form.value.roomTypeId != null) {
        updateRoom_type(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRoom_type(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _roomTypeIds = row.roomTypeId || ids.value
  proxy.$modal.confirm('是否确认删除房型信息编号为"' + _roomTypeIds + '"的数据项？').then(function() {
    return delRoom_type(_roomTypeIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 床型配置序号 */
function rowBedConfigIndex({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 床型配置添加按钮操作 */
function handleAddBedConfig() {
  let obj = {}
  obj.bedType = ""
  obj.bedQuantity = ""
  obj.bedSize = ""
  bedConfigList.value.push(obj)
}

/** 床型配置删除按钮操作 */
function handleDeleteBedConfig() {
  if (checkedBedConfig.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的床型配置数据")
  } else {
    const bedConfigs = bedConfigList.value
    const checkedBedConfigs = checkedBedConfig.value
    bedConfigList.value = bedConfigs.filter(function(item) {
      return checkedBedConfigs.indexOf(item.index) == -1
    })
  }
}

/** 复选框选中数据 */
function handleBedConfigSelectionChange(selection) {
  checkedBedConfig.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('businessInfo/room_type/export', {
    ...queryParams.value
  }, `room_type_${new Date().getTime()}.xlsx`)
}

getList()
</script>
